package com.zuosh.service;

import com.zuosh.common.ResponseCode;
import com.zuosh.common.Result;
import com.zuosh.dao.UserMapper;
import com.zuosh.domain.User;
import com.zuosh.domain.UserExample;
import com.zuosh.domain.enums.UserStatusEnum;
import com.zuosh.utils.ListsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.zuosh.utils.StringUtility.stringNoValue;

/**
 * Created by zuoshuai on 2017/3/17.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<Long> register(User user) {
        Result<Long> result = new Result<>();
        if (user == null || stringNoValue(user.getUsername()) || stringNoValue(user.getPassword())
                || stringNoValue(user.getNickName())) {
            return result.setResult(ResponseCode.PARAMETER_ERROR, null, "参数存在空值");
        }
        //check exists
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(user.getUsername());
        example.or(example.createCriteria().andNickNameEqualTo(user.getNickName()));
        List<User> users = userMapper.selectByExample(example);
        if (ListsUtil.isNotEmpty(users)) {
            return result.setResult(ResponseCode.BIZ_ERROR, null, "用户或昵称已存在");
        }
        //add
        userMapper.insertSelective(user);
        return result.setResult(ResponseCode.SUCCESS, user.getId(), "ok");
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public Result<User> login(User user) {
        Result<User> result = new Result<>();
        if (user == null || stringNoValue(user.getUsername()) || stringNoValue(user.getPassword())) {
            return result.setResult(ResponseCode.PARAMETER_ERROR, null, "参数存在空值");
        }
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(user.getUsername()).andPasswordEqualTo(user.getPassword()).
                andStatusEqualTo(UserStatusEnum.actived.getStatus());
        List<User> users = userMapper.selectByExample(userExample);
        if (ListsUtil.isNotEmpty(users)) {
            return result.setResult(ResponseCode.SUCCESS, users.get(0), "ok");
        } else {
            return result.setResult(ResponseCode.EMPTY_RESULT, null, "no user");
        }
    }

    /**
     * 激活用户
     *
     * @param user
     * @return
     */
    @Override
    public Result<Boolean> activeUser(User user) {
        Result<Boolean> result = new Result<>();
        if (user == null || stringNoValue(user.getUsername())) {
            return result.setResult(ResponseCode.PARAMETER_ERROR, null, "用户名不可为空");
        }
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(user.getUsername());
        List<User> users = userMapper.selectByExample(example);
        if (ListsUtil.isEmpty(users)) {
            return result.setResult(ResponseCode.BIZ_ERROR, null, "无此账户");
        }
        //
        for (User u : users) {
            if (u.getStatus() == UserStatusEnum.actived.getStatus()) {
                return result.setResult(ResponseCode.SUCCESS, false, "用户已激活,不需再次激活");
            }
        }
        User user1 = users.get(0);
        user1.setStatus(UserStatusEnum.actived.getStatus());
        userMapper.updateByExampleSelective(user1, example);
        return result.setResult(ResponseCode.SUCCESS, true, "激活成功");
    }
}
